//
//  LGFollowHandleView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/17.
//  处理意见--输入、录音

#import "LGFollowHandleView.h"
#import <AVFoundation/AVFoundation.h>
#import "LGFollowRecordItemView.h"
#import "MCPlayer.h"
#define placeHolder  @"请输入..."

@interface LGFollowHandleView()<UITextViewDelegate,LGFollowRecordItemDelegate,MCPlayerDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;
/**   */
@property (nonatomic , assign)NSInteger playIndex;

@end

@implementation LGFollowHandleView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setRecordArray:(NSArray *)recordArray{
    _recordArray = recordArray;
    for (NSInteger i=0; i<self.itemArray.count; i++) {
        LGFollowRecordItemView *itemView = self.itemArray[i];
        [itemView removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    for (NSInteger i=0; i<recordArray.count; i++) {
        LGFollowRecordItemView *itemView = [[LGFollowRecordItemView alloc]init];
        itemView.audioUrl = LGNSString(recordArray[i][@"url"]);
        itemView.duration = LGNSString(recordArray[i][@"duration"]);
        itemView.deleteBtn.tag = 1000+i;
        itemView.canDelete = YES;
        itemView.delegate = self;
        itemView.index = i;
        [itemView.deleteBtn addTarget:self action:@selector(deleteRecordAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:itemView];
        [self.itemArray addObject:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentTF.mas_bottom).offset(viewPix(12)+(viewPix(40)*i));
            make.left.equalTo(self).offset(viewPix(16));
            make.width.equalTo(@(viewPix(175)));
            make.height.equalTo(@(viewPix(32)));
        }];
    }
    if(self.recordViewHeightChanged){
        CGFloat viewHeight = kMaxY(self.contentTF.frame)+viewPix(12)+viewPix(40)*recordArray.count+viewPix(44);
        self.recordViewHeightChanged(viewHeight);
    }
}

-(void)playVoiceWithUrl:(NSString *)url index:(NSInteger)index{
    self.playIndex = index;
    for (NSInteger i = 0; i<self.itemArray.count; i++) {
        LGFollowRecordItemView *itemView = self.itemArray[i];
        itemView.playing = (i == index)?YES:NO;
    }
    MCPlayer *player = [MCPlayer shareManager];
    [player playMediaWithUrl:url tempPath:nil desPath:nil delegate:self];
    player.delegate = self;
    if(self.startPlayVoice){
        self.startPlayVoice();
    }
}

-(void)playerPlayTimeSecond:(CGFloat)seconds duration:(CGFloat)duration currentStr:(NSString *)currentString withResidueStr:(NSString *)residueStr player:(NSObject *)player{
    if(seconds == duration){
        LGFollowRecordItemView *itemView = self.itemArray[self.playIndex];
        itemView.playing = NO;
    }
}

-(void)stopPlayVoice{
    for (NSInteger i = 0; i<self.itemArray.count; i++) {
        LGFollowRecordItemView *itemView = self.itemArray[i];
        itemView.playing = NO;
    }
}

//删除录音
-(void)deleteRecordAction:(UIButton *)sender{
    NSInteger index = sender.tag-1000;
    if(self.removeRecordData){
        self.removeRecordData(index);
    }
}

-(void)setContent:(NSString *)content{
    self.contentTF.text = content;
    if(content.length == 0 || [content isEqualToString:placeHolder]){
        self.contentTF.text = placeHolder;
        self.contentTF.textColor = [UIColor colorWithString:@"#999999"];
    }else{
        self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
    }
    if(self.content.length == 0){
        [self textViewDidChange:self.contentTF];
    }
    _content = content;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
    if([textView.text isEqualToString:placeHolder]){
        self.contentTF.text = @"";
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    self.content = textView.text;
}

- (void)textViewDidChange:(UITextView *)textView{
    NSString *lang = textView.textInputMode.primaryLanguage; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) { // 没有高亮
            if (textView.text.length>500) { // 输入的文字大于设定的长度  这里设置的是200个字符
                textView.text = [textView.text substringToIndex:500];
            }else{
                textView.text = textView.text;
            }
           
            if(textView.text.length>0){
                textView.attributedText = [textView.text attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} lineSpacing:5];
            }
            CGSize maxSize = CGSizeMake(Screen_W-viewPix(74), MAXFLOAT);
            CGSize fitSize = [textView sizeThatFits:maxSize];
            CGFloat newHeight = (fitSize.height>viewPix(50))?fitSize.height:viewPix(50);
            [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(newHeight));
            }];
            if(self.recordViewHeightChanged){
                CGFloat viewHeight = viewPix(45)+newHeight+viewPix(12)+viewPix(40)*self.recordArray.count+viewPix(44);
                self.recordViewHeightChanged(viewHeight);
            }
        }
    }else{
        if (textView.text.length>500) {
            textView.text = [textView.text substringToIndex:500];
        }else{
            textView.text = textView.text;
        }
        if(textView.text.length>0){
            textView.attributedText = [textView.text attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} lineSpacing:5];
        }
        CGSize maxSize = CGSizeMake(Screen_W-viewPix(74), MAXFLOAT);
        CGSize fitSize = [textView sizeThatFits:maxSize];
        CGFloat newHeight = (fitSize.height>viewPix(50))?fitSize.height:viewPix(50);
        [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(newHeight));
        }];
        if(self.recordViewHeightChanged){
            CGFloat viewHeight = viewPix(45)+newHeight+viewPix(12)+viewPix(40)*self.recordArray.count+viewPix(44);
            self.recordViewHeightChanged(viewHeight);
        }
    }
}
#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self addSubview:self.titleLabel];
    [self addSubview:self.contentTF];
    [self addSubview:self.recordBtn];
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(viewPix(45));
        make.left.equalTo(self).offset(viewPix(16));
        make.right.equalTo(self).offset(-viewPix(16));
        make.height.equalTo(@(viewPix(50)));
    }];
    [self.recordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-viewPix(12));
        make.bottom.equalTo(self).offset(-viewPix(12));
        make.width.equalTo(@(viewPix(100)));
        make.height.equalTo(@(viewPix(28)));
    }];
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(16), viewPix(16), viewPix(170), 23);
    }
    return _titleLabel;
}

-(UITextView *)contentTF{
    if(!_contentTF){
        _contentTF = [[UITextView alloc]init];
        _contentTF.backgroundColor = [UIColor colorWithString:@"#F0F7FF"];
        _contentTF.textContainerInset = UIEdgeInsetsMake(8, 8, 8, 8);
        _contentTF.tintColor = [UIColor colorWithString:@"#333333"];
        _contentTF.textColor = [UIColor colorWithString:@"#999999"];
        _contentTF.textAlignment = NSTextAlignmentLeft;
        _contentTF.text = placeHolder;
        _contentTF.font = LGFont(14);
        _contentTF.cornerRidus = 7;
        _contentTF.delegate = self;
    }
    return _contentTF;
}

-(UIButton *)recordBtn{
    if(!_recordBtn){
        _recordBtn = [UIButton buttonWithTitle:@"长按录音" titleFont:LGFontWeight(14, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@"处理意见-长按录音"];
        _recordBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -viewPix(5), 0, 0);
        _recordBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(6), 0, 0);
        _recordBtn.backgroundColor = [UIColor colorWithString:@"#3291FF"];
        _recordBtn.cornerRidus = viewPix(14);
    }
    return _recordBtn;
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}


@end
